package dao

import (
	"go-frame/src/server-main/models"

	"gorm.io/gorm"
)

type ProductDAO struct {
	DB *gorm.DB
}

func NewProductDAO(db *gorm.DB) *ProductDAO {
	return &ProductDAO{DB: db}
}

func (dao *ProductDAO) GetAllProducts() ([]models.Product, error) {
	var products []models.Product
	result := dao.DB.Find(&products)
	return products, result.Error
}

func (dao *ProductDAO) CreateProduct(product *models.Product) error {
	result := dao.DB.Create(product)
	return result.Error
}

func (dao *ProductDAO) GetProductByID(id uint) (models.Product, error) {
	var product models.Product
	result := dao.DB.First(&product, id)
	return product, result.Error
}

func (dao *ProductDAO) UpdateProduct(product *models.Product) error {
	result := dao.DB.Save(product)
	return result.Error
}

func (dao *ProductDAO) DeleteProduct(id uint) error {
	result := dao.DB.Delete(&models.Product{}, id)
	return result.Error
}
